//
//  CityViewController.m
//  Travel assistant
//
//  Created by 杨帆 on 16/3/2.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "CityViewController.h"
#import "CityHeardView.h"
#import "CityModel.h"

#define kwidth [UIScreen mainScreen].bounds.size.width

#define kheight [UIScreen mainScreen].bounds.size.height
@interface CityViewController ()<UISearchControllerDelegate,UISearchResultsUpdating,UITableViewDelegate,UITableViewDataSource>


@property (nonatomic,strong)CityHeardView *heardV;


@property (nonatomic,strong) UITableView *tableView;



@property (nonatomic,strong) NSMutableDictionary *dataDictionary;

@property (nonatomic,strong) NSMutableArray *groupNameArray;


@end

@implementation CityViewController




-(NSMutableDictionary *)dataDictionary{
    
    if (!_dataDictionary) {
        
        _dataDictionary =[NSMutableDictionary dictionary];
    }
    return _dataDictionary;
}


-(NSMutableArray *)groupNameArray{
    
    if (!_groupNameArray) {
        
        _groupNameArray = [NSMutableArray array];
    }
    return _groupNameArray;
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.title = [NSString stringWithFormat:@"%@",self.titleName];
    
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加HeardView
    [self addHeardView];
    
    [self addTableView];
    
    [self makeDataSourceForTableViewWithAreas:@"areas"];
    
}

//设置搜索栏和segment
-(void)addHeardView{

    self.heardV = [[CityHeardView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 80)];
    [self.view addSubview:self.heardV];

    self.heardV.search.searchBar.placeholder = @"搜索";
    //设置搜索栏的代理
    self.heardV.search.delegate = self;
    
    self.heardV.search.searchResultsUpdater = self;
    
    
    [self.heardV.segment addTarget:self action:@selector(ClickCountryAction:) forControlEvents:UIControlEventValueChanged];
    
    
    
}
//设置TableView
-(void)addTableView{
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 144, kwidth, kheight-188) style:(UITableViewStylePlain)];
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    
    [self.view addSubview:self.tableView];
    
    
    
    
}

#pragma mark=====UISearchResultsUpdating方法

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    
    
    
    
    
    
}

//制作数据
-(void)makeDataSourceForTableViewWithAreas:(NSString *)areas{
    

    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"city" ofType:@"plist"];
    
     NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    
    NSArray *tempArray = dictionary[areas];

    for (NSDictionary *dict in tempArray) {
        
        NSString *groupName = dict[@"name"];
        if ([groupName isEqualToString:@"热门城市"]) {
            
            groupName = @"#";
        }
        
        NSMutableArray *arr = [NSMutableArray array];
        
        for (NSDictionary *dic in dict[@"leaves"]) {
            
            CityModel *model = [[CityModel alloc]initWithDictionary:dic];
            
            [arr addObject:model];
            
            [self.dataDictionary setObject:arr forKey:groupName];

        }
        
       
    }
    
    
    [self.groupNameArray addObjectsFromArray:self.dataDictionary.allKeys];
    
    [self.groupNameArray sortUsingSelector:@selector(compare:)];
    
//   NSLog(@"%@",_groupArray);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
    
    
    

}






//切换segment
-(void)ClickCountryAction:(UISegmentedControl *)sender{
    
    
    if (sender.selectedSegmentIndex == 0) {
        
        self.groupNameArray = [NSMutableArray array];
        
        self.dataDictionary = [NSMutableDictionary dictionary];
        
        [self makeDataSourceForTableViewWithAreas:@"areas"];
        
    }else{
        
        self.dataDictionary = [NSMutableDictionary dictionary];
        
        self.groupNameArray = [NSMutableArray array];
     
        [self makeDataSourceForTableViewWithAreas:@"areas2"];
    }
    
}














#pragma mark===========tableview实现的方法===========

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
   
    
    return self.groupNameArray;
    
}


//分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return self.groupNameArray.count;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return [self.dataDictionary[self.groupNameArray[section]] count];
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    
    if (section == 0) {
        
        return @"热门城市";
    }
    
    return self.groupNameArray[section];
    
}


//创建cell，赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cellID"];
    }
    
    
    CityModel *model = self.dataDictionary[self.groupNameArray[indexPath.section]][indexPath.row];
    
    
    
    if (self.heardV.segment.selectedSegmentIndex == 1) {
        
        
        cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)",model.cityName,model.countryName];
    }else{
        
        cell.textLabel.text = [NSString stringWithFormat:@"%@",model.cityName];
    }
    
    
    
    return cell;
}





//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 40;
    
}






//cell点击事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CityModel *model = self.dataDictionary[self.groupNameArray[indexPath.section]][indexPath.row];
    
    self.passBlock(model,self.title);
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
















@end
